//
//  DictionaryTest.m
//  MacTestProject
//
//  Created by LiuLichen on 2022/4/11.
//

#import "DictionaryTest.h"

@implementation DictionaryTest

- (id) init {
    self = [super init];

    [self testInit];
    [self testOperation];

    return self;
}

- (void) testInit {
    NSDictionary* dict0 = @{@"name": @"jack",
                            @"age": @"20",
                            @"sex": @"mail"};
    NSLog(@"%@", dict0);
    NSDictionary* dict1 = [NSDictionary dictionary];
    NSLog(@"%@", dict1);
    NSDictionary* dict2 = [NSDictionary dictionaryWithObject: @"jack" forKey: @"name"];
    NSLog(@"%@", dict2);
    NSDictionary* dict3 = [NSDictionary dictionaryWithObjectsAndKeys: @"sam", @"name",
                                                                      @"25", @"age",
                                                                      @"male", @"sex",
                                                                      nil];
    NSLog(@"%@", dict3);
    NSDictionary* dict4 = [NSDictionary dictionaryWithObjects: @[@"lucy", @"18", @"female"]
                                                      forKeys: @[@"name", @"age", @"sex"]];
    NSLog(@"%@", dict4);
}

- (void) testOperation {
    {
        NSDictionary* dict = @{@"name": @"tina",
                               @"age": @"22",
                               @"sex": @"female"};
        id obj = dict[@"age"];
        NSLog(@"the age property in dict is %@", obj);
        obj = [dict objectForKey: @"name"];
        NSLog(@"the name property in dict is %@", obj);
        for (NSString* key in dict) {
            NSLog(@"%@ is %@", key, dict[key]);
        }
        [dict enumerateKeysAndObjectsUsingBlock:
             ^(NSString* key, NSString* val, BOOL* stop) {
            NSLog(@"enumerate key: %@, val: %@", key, val);
        }];
    }
    {
        NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjects: @[@"lucy", @"18", @"female"]
                                                                       forKeys: @[@"name", @"age", @"sex"]];
        [dict setObject: @"artist" forKey: @"job"];
        for (NSString* key in dict) {
            NSLog(@"after setObject, %@ is %@", key, dict[key]);
        }
        [dict removeObjectForKey: @"age"];
        for (NSString* key in dict) {
            NSLog(@"after removeObject, %@ is %@", key, dict[key]);
        }
    }
}

@end
